<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="120px">
            <el-form-item label="文件名称" prop="assignName">
                <el-input
                    v-model="queryParams.fileName"
                    placeholder="请输入文件名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="recordList">
            <el-table-column label="文件名称" align="center" prop="fileName">
                <template slot-scope="scope">
                    <el-link type="primary" @click="viewPdf(scope.row)">{{ scope.row.fileName }}</el-link>
                </template>
            </el-table-column>
            <el-table-column label="分配时间" align="center" prop="assignTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.assignTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="文件类型" align="center" prop="fileType">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.dcc_file_type" :value="scope.row.fileType"/>
                </template>
            </el-table-column>
            <el-table-column label="查看状态" align="center" prop="viewStatus">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.assign_view_status" :value="scope.row.viewStatus"/>
                </template>
            </el-table-column>
            <el-table-column label="紧急程度" align="center" prop="level">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.dcc_emergency_level" :value="scope.row.level"/>
                </template>
            </el-table-column>
            <el-table-column label="到期时间" align="center" prop="dueDate" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.dueDate) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        icon="el-icon-view"
                        size="mini"
                        @click="() => viewPdf(scope.row)"
                    > 查看文件
                    </el-button>
                </template>
            </el-table-column>
            <el-table-column label="签阅" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-switch
                        active-color="#13ce66"
                        inactive-color="#ff4949"
                        :value="scope.row.viewStatus === 'acknowledged'"
                        :disabled="scope.row.viewCountSwitch"
                        @change="signChanged(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>
        </el-table>
        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
    </div>
</template>

<script>
import {listRecord, sign, view} from "@/api/business/assignRecord";
import {parseTime} from "../../../utils/ruoyi";
import {downloadBlob} from "@/utils/request";

export default {
    name: "userViewRecord",
    dicts: ['dcc_file_type', 'assign_type', 'dcc_emergency_level', 'assign_view_status'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 文件浏览记录表格数据
            recordList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                sender: null,
                assignId: null,
                assignName: null,
                assignType: null,
                fileId: null,
                fileName: null,
                filePath: null,
                assignTime: null,
                fileType: null,
                viewStatus: null,
                level: null,
                dueDate: null,
                email: null
            },
            // 表单参数
            form: {},
        };
    },
    created() {
        this.getList();
    },
    methods: {
        signChanged(item){
            sign(item).then(response => {
                this.$message.success(response.msg);
                this.getList();
            })
        },
        // 点击预览按钮，查看pdf
        viewPdf(item) {
            view(item).then(response => {
                downloadBlob("/common/downloadAny", {'fileName': item.filePath+"\\"+item.fileName}, item.fileName).then(res => {
                    const binaryData = [];
                    binaryData.push(res);
                    //获取blob链接
                    this.pdfUrl = window.URL.createObjectURL(new Blob(binaryData, {type: 'application/pdf'}));
                    window.open(this.pdfUrl);
                    window.URL.revokeObjectURL(this.pdfUrl);
                })
                this.getList();
            })

        },
        parseTime,
        /** 查询文件浏览记录列表 */
        getList() {
            this.user = this.$store.state.user;
            this.queryParams.assignId = this.user.userId;
            this.loading = true;
            this.queryParams.fileAssignId = this.fileAssignId
            const type = this.$route.params.type;
            if (type === 'unview') {
                this.queryParams.viewStatus = 'pending';
            } else if (type === 'view') {
                this.queryParams.viewStatus = 'acknowledged';
            }
            listRecord(this.queryParams).then(response => {
                this.recordList = response.rows;
                this.recordList.forEach(item => {
                    item.viewCountSwitch = item.viewCount === 0 || item.viewStatus === 'acknowledged';
                })
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                sender: null,
                assignId: null,
                assignName: null,
                assignType: null,
                fileId: null,
                fileName: null,
                filePath: null,
                assignTime: null,
                fileType: null,
                viewStatus: null,
                level: null,
                dueDate: null,
                createTime: null,
                createBy: null,
                updateTime: null,
                updateBy: null,
                remark: null,
                email: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },

    }
};
</script>
